# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import copy   # 该模块提供了用于执行对象的浅拷贝（shallow copy）和深拷贝（deep copy）的功能。拷贝是创建对象的一个或多个副本的过程，但拷贝的深度（即拷贝的层次）会影响副本与原始对象之间的独立性。
fig = plt.figure()
plt.rcParams['font.sans-serif']=['STKAITI'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
data=[80,90,100,150,300,250,1600,230,200,210,170,400,-800,500,530,550]

#等深度分箱
def Same_High(H,data=[]):
    data=np.array(data)
    Sort_index=np.argsort(data)
    Box=[]
    Box_th=0
    Lave_num=data.shape[0]
    i=0
    Box.append([])
    while i<data.shape[0]:
        if Lave_num>H or Lave_num==H:
            for t in range(H):
                Box[Box_th].append(Sort_index[i])
                i=i+1
        else:
            for t in range(Lave_num):
                Box[Box_th].append(Sort_index[i])
                i=i+1
        Lave_num=Lave_num-H
        Box.append([])
        Box_th=Box_th+1
    Box.remove(Box[-1])
#     print u'分箱：',Box
    return Box
#平均值平滑
def mean_smooth(index):
    index=np.array(index)
    smooth=copy.deepcopy(data)
    for i in range(index.shape[0]):
        x=[]
        index[i]=np.array(index[i])
        for j in range(index[i].shape[0]):
            x.append(smooth[index[i][j]])
        mean=np.mean(x)
        for j in range(index[i].shape[0]):
            smooth[index[i][j]]=mean
    return smooth
#中位数平滑
def median_smooth(index):
    index=np.array(index)
    smooth=copy.deepcopy(data)
    for i in range(index.shape[0]):
        x=[]
        index[i]=np.array(index[i])
        for j in range(index[i].shape[0]):
            x.append(smooth[index[i][j]])
        median=np.median(x)
        for j in range(index[i].shape[0]):
            smooth[index[i][j]]=median
    return smooth
#边界值平滑
def boundary_smooth(index):
    index=np.array(index)
    smooth=copy.deepcopy(data)
    for i in range(index.shape[0]):
        index[i]=np.array(index[i])
        if index[i].shape[0]>3 or index[i].shape[0]==3:
            for j in range(1,index[i].shape[0]-2):
                a=smooth[index[i][j]]-smooth[index[i][0]]
                b=smooth[index[i][index[i].shape[0]-1]]-smooth[index[i][j]]
                if a>b:
                    smooth[index[i][j]]=smooth[index[i][index[i].shape[0]-1]]
                else:
                    smooth[index[i][j]]=smooth[index[i][0]]
    return smooth
# n=int(raw_input("please input the High:"))
n=4
#三种平滑后数据对比
# print data
# print mean_smooth(Same_High(n,data))
# print median_smooth(Same_High(n,data))
# print boundary_smooth(Same_High(n,data))

#三种平滑后折线图对比
x1=range(0,len(data))
y_mean=mean_smooth(Same_High(n,data))
x2=range(0,len(data))
y_median=median_smooth(Same_High(n,data))
x3=range(0,len(data))
y_bound=boundary_smooth(Same_High(n,data))
x4=range(0,len(data))
y_data=data

plt.plot(x4,data,label=u'原始数据',linewidth=2,color='k',marker='^')
plt.plot(x1,y_mean,label=u'均值平滑',linewidth=3,color='k',linestyle=':',marker='o')
plt.plot(x2,y_median,label=u'中值平滑',linewidth=3,color='k',linestyle='--',marker='.')
plt.plot(x3,y_bound,label=u'边界值平滑',linewidth=3,color='blue',linestyle='-.',markerfacecolor='k',markersize=12)

plt.xlabel(u'序列')
plt.ylabel(u'数值')
plt.title(u'平滑前后对比')
plt.legend()
plt.show()
# fig.savefig('./img/Binning.png',dpi=600)

